@echo off
REM 用来检查当前网络是否是想要连接的网络（wlanname）
set wlanname=CMCC-NyTx
::set wlanname=lin
echo wlan is %wlanname%
set nowWlanstate=None
echo %nowWlanstate%
rem 判断当前连接的网络
call :findwlan %wlanname% 
if "%nowWlanstate%"=="success" (goto wlanRight
)else (echo The current network is wrong, start fixing the network)
rem 查看配置库是否有对应的网络文件
call :judgewlan %wlanname%
if "%nowWlanstate%"=="success" (echo wlan profiles exist
)else (goto wlanNoExist)
rem 连接上对应的网络
call :connectwlan %wlanname% 
rem 再次判断当前连接的网络
call :findwlan %wlanname% 
if "%nowWlanstate%"=="success" (echo wlan change success
)else (echo wlan change fail)

::pause
exit


:findwlan
echo findwlan %1
rem 查看当前连接的网络是否正确
set wlanneme=%1
netsh wlan show interfaces | find "%wlanneme%" > nul && call :WriteWlanstateSuccess %wlanneme% || call :WriteWlanstateError %wlanneme%
exit /b

:judgewlan
echo judgewlan %1
rem 判断网络是否存在对应的配置文件
set judgewlanname=%1
netsh wlan show profiles | find "%judgewlanname%" > nul && call :WriteWlanstateSuccess %judgewlanname% || call :WriteWlanstateError %judgewlanname%
exit /b

:connectwlan
echo connectWlan %1
rem 连接wifi
set wlanname=%1
netsh wlan connect name=%wlanname% ssid=%wlanname%
exit /b

:wlanRight
echo now wlan is right
::pause
exit

:wlanNoExist
echo wlan no exist
::pause
exit

:WriteWlanstateSuccess
echo %1 success
set nowWlanstate=success
exit /b

:WriteWlanstateError
echo %1 error
set nowWlanstate=error
exit /b